<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微博主页</title>
    <link rel="stylesheet" href="css/microblog_list.css">
    <link rel="stylesheet" href="css/common.css">
</head>
<body>
<div class="nav">
    <img src="image/微博图标.jpeg" alt="">
    <span class="title">微博管理系统</span>

    <span class="spacer"></span>

    <a href="microblog_search.html">搜索</a>
    <a href="microblog_list.html">新鲜事</a>
    <a href="microblog_edit.html">写微博</a>
    <a href="logout">退出</a>
</div>

<div class="container">
    <div class="container-left">
        <div class="card">
            <img src="image/微博图标.jpeg" alt="">
            <h3>小蔡</h3>
            <div class="counter">
                <span>关注</span>
                <span>粉丝</span>
            </div>
            <div class="counter">
                <span>16</span>
                <span>50</span>
            </div>
            <div class="counter">
                <span>文章</span>
                <span>阅读量</span>
            </div>
            <div class="counter">
                <span>98</span>
                <span>50k</span>
            </div>
            <div class="counter">
                <span>成就</span>
            </div>
        </div>
    </div>

    <div class="container-right">
<!--                    <div class="blog">-->
<!--                        <div class="title">这是第一篇博客</div>-->
<!--                        <div class="date">2022-09-19 12:00:00</div>-->
<!--                        <div class="desc">从今天起我要认真敲代码. Lorem ipsum dolor sit amet consectetur adipisicing elit. Incidunt amet nobis sed assumenda laboriosam nam ex cupiditate sequi ullam doloremque eius corporis, veritatis est ipsa nesciunt eveniet. Reprehenderit, error consequatur.</div>-->
<!--                        <a href="#">查看全文 &gt;&gt; </a>-->
<!--                    </div>-->
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script src="js/app.js"></script>
<script>
    // 通过这个函数, 来从服务器获取到博客列表的数据
    function getBlogs() {
        $.ajax({
            type: 'get',
            url: 'blog',
            success: function(body) {
                // 根据返回的 json 数据, 来构造出页面内容, div.blog
                // jquery ajax 会自动的把响应得到的 body 按照响应的 Content-Type 进行转换格式.
                // 如果响应的 Content-Type 是 json, 此时就会自动把 body 转成 js 的对象
                let container = document.querySelector('.container-right');
                for (let blog of body) {
                    let blogDiv = document.createElement('div');
                    blogDiv.className = 'blog';
                    // 创建博客标题
                    let titleDiv = document.createElement('div');
                    titleDiv.className = 'title';
                    titleDiv.innerHTML = blog.title;
                    blogDiv.appendChild(titleDiv);
                    // 创建日期
                    let dateDiv = document.createElement('div');
                    dateDiv.className = 'date';
                    dateDiv.innerHTML = blog.postTime;
                    blogDiv.appendChild(dateDiv);
                    // 创建摘要
                    let descDiv = document.createElement('div');
                    descDiv.className = 'desc';
                    descDiv.innerHTML = blog.content;
                    blogDiv.appendChild(descDiv);
                    // 创建查看全文按钮
                    let a = document.createElement('a');
                    a.innerHTML = '查看全文 >>';
                    a.href = 'blog_detail.html?blogId=' + blog.blogId;
                    blogDiv.appendChild(a);
                    // 把 blogDiv 加入外层元素
                    container.appendChild(blogDiv);
                }
            }
        });
    }

    function getUserInfo(){
        $.ajax({
            type: 'get',
            url: 'userInfo',
            success: function(body){
                //让后端在查询失败的时候, 不要返回200, 而是返回403
                //避免在前面这里触发success分支
                let h3 = document.querySelector('.card>h3');
                h3.innerHTML = body.username;
            }
        });
    }

    //获取博客列表
    getBlogs();

    // 验证登录状态
    //checkLogin();

    //获取当前用户信息
    //getUserInfo();

</script>
</body>
</html>